package com.inibb.app.service.admin.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inibb.app.converter.Db2VOConverter;
import com.inibb.app.converter.SaSystemParamConverter;
import com.inibb.app.dao.JpaGenericDao;
import com.inibb.app.dao.api.IDao;
import com.inibb.app.pagination.dto.PaginationCriteria;
import com.inibb.app.po.admin.SaSystemParam;
import com.inibb.app.service.admin.ISaSystemParamService;
import com.inibb.app.vo.SysParamSC;
import com.inibb.app.vo.admin.SaSystemParamVO;

@Transactional
@Service
public class SaSystemParamServiceImpl implements ISaSystemParamService {

	private static final long serialVersionUID = 1L;

	private IDao<SaSystemParam, Integer> dao;

	@PersistenceContext(unitName = "entityManager")
	private EntityManager entityManager ;
	
	@Autowired
	public void setEntityManagerFactory() {
		dao = new JpaGenericDao<SaSystemParam, Integer>(entityManager,SaSystemParam.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaSystemParamVO> searchSystemParam() {
		SysParamSC paginationCriteria = new SysParamSC();
		paginationCriteria.setStartRow(0);
		paginationCriteria.setPageSize(Integer.MAX_VALUE);
		List<Object[]> list = (List<Object[]>) dao.queryNativeSqlForList("admin", "searchPageSysParam", paginationCriteria);
		List<SaSystemParamVO> gcList = new ArrayList<SaSystemParamVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4SysParam(obj));
		}
		return gcList;
	}

	@Override
	public void saveOrUpdate(SaSystemParamVO vo) {
		if(vo.getParamId()==null)
			dao.persist(SaSystemParamConverter.toPO(vo));
		else
			dao.merge(SaSystemParamConverter.toPO(vo));
	}

	@Override
	public void delete(SaSystemParamVO vo) {
		dao.delete(vo.getParamId());
	}

	@Override
	public void delete(Integer id) {
		dao.delete(id);
	}

	public int countPageSystemParams(SysParamSC paginationCriteria) {
		BigInteger total = (BigInteger) dao.queryNativeSqlForObject("admin", "countPageSysParam", paginationCriteria);
		
		return total.intValue();
	}

	public List<SaSystemParamVO> searchPageSystemParams(SysParamSC paginationCriteria) {
		List<Object[]> list = (List<Object[]>) dao.queryNativeSqlForList("admin", "searchPageSysParam", paginationCriteria);
		List<SaSystemParamVO> gcList = new ArrayList<SaSystemParamVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4SysParam(obj));
		}
		return gcList;
	}

	public int countPageSystemParams(PaginationCriteria paginationCriteria) {
		BigInteger total = (BigInteger) dao.queryNativeSqlForObject("admin", "countPageSysParam", paginationCriteria);
		
		return total.intValue();
	}

	public List<SaSystemParamVO> searchPageSystemParams(PaginationCriteria paginationCriteria) {
		List<Object[]> list = (List<Object[]>) dao.queryNativeSqlForList("admin", "searchPageSysParam", paginationCriteria);
		List<SaSystemParamVO> gcList = new ArrayList<SaSystemParamVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4SysParam(obj));
		}
		return gcList;
	}

	@Override
	public SaSystemParamVO getSystemParam(Integer id) {

		return SaSystemParamConverter.toVO(dao.get(id));
	}
}
